#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    //显示密码按钮
    connect(ui->checkBox, &QPushButton::clicked, this, &MainWindow::on_checkBox_clicked);

}

MainWindow::~MainWindow()
{
    delete ui;
}

//显示密码按钮，1表示选中，0表示密文显示
void MainWindow::on_checkBox_clicked(bool checked)
{
    if (checked)
    {
        //显示密码
        ui->lineEdit_2->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        //密文显示
        ui->lineEdit_2->setEchoMode(QLineEdit::Password);
    }
}

//连接数据库
void MainWindow::connectDB()
{
    //连接数据库
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("testDB.db");//数据库名称
    db.setUserName("root");//用户名
    db.setPassword("123456");//密码
    bool ok = db.open();
    if(ok)
    {
        //QMessageBox::information(this, "提示","数据库连接成功");
        //qDebug()<<"数据库连接成功";
    }
    else
    {
        //QMessageBox::information(this, "提示","数据库连接失败");
        this->close();
        //qDebug()<<"数据库连接失败";
    }
}


//注册
void MainWindow::on_pushButton_clicked()
{
    emit ToregisterSignal();
}

//登录
void MainWindow::on_pushButton_2_clicked()
{
    connectDB();
    QString account = ui->lineEdit->text();//用户名
    QString pwd = ui->lineEdit_2->text();//密码
    QSqlQuery query;
    QString temp=QString::fromUtf8("select pwd from user where account ='%1'").arg(account);
    query.exec(temp);
    query.next();
    if(query.value("pwd").toString() ==  pwd)
    {
        qDebug()<<"登录成功!";
        emit ToxinxiSignal();
    }
    else
    {
        QMessageBox qm(this);
        qm.setText(QStringLiteral("账号或密码错误!"));
        qm.move(this->geometry().center());
        qm.exec();
        qDebug()<<"登录失败!";
        //qDebug()<<"登陆失败原因:"<<query.lastError();
    }
}

